﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Client.Service;
using GenericModels;

namespace Client
{
    public partial class UserCtlMovieSelect : UserControl
    {
        public event UserControlClickHandler InnerButtonClick;
        public delegate void UserControlClickHandler(object sender, EventArgs e);
        public Booking booking { get; set; }

        private IShow showService;
        private IBooking bookingService;
        private IPerson personService;
        private IList<Show> list;
        private Dictionary<Button, Show> buttons;
        private Employee employee;
        private Customer customer;

        //Constructor if booking already exists and more partbookings are to be added
        public UserCtlMovieSelect(Booking booking, Employee employee, Customer customer)
        {
            InitializeComponent();
            this.booking = booking;
            this.employee = employee;
            this.customer = customer;

            showService = new ShowClient();
            bookingService = new BookingClient("WSHttpBinding_IBooking");
            personService = new PersonClient("WSHttpBinding_IPerson");

            list = showService.GetAllShows();
            buttons = new Dictionary<Button, Show>();

            DisplayShows();
            LoadEmployeePic();
            LoadBookingInfo();
        }

        //Constructor used for completely new booking
        public UserCtlMovieSelect(Employee employee, Customer cus)
        {
            InitializeComponent();

            this.employee = employee;
            this.customer = cus;

            showService = new ShowClient("WSHttpBinding_IShow");
            bookingService = new BookingClient("WSHttpBinding_IBooking");
            personService = new PersonClient("WSHttpBinding_IPerson");

            list = showService.GetAllShows();
            buttons = new Dictionary<Button, Show>();

            CreateLocals();
            
            DisplayShows();
            LoadEmployeePic();
            LoadBookingInfo();
        }

        private void DisplayShows()
        {

            foreach (Show s in list)
            {
                //PictureBoxSetup - create and set the properties of the image
                //ButtonSetup - create and set the properties of the Buttons
                //AssemblePanels - Assembles the controls in the TableLayoutPanel and FlowLayoutPanel respectively
                AssemblePanels(PictureBoxSetup(s), ButtonSetUp(s));

            }

            //add the FlowLayoutPanel to the window main panel
            this.Controls.Add(panelMovieContainer);
        }

        /**
         * Create and set the properties of the image
         * */
        private PictureBox PictureBoxSetup(Show s)
        {
            PictureBox box = new PictureBox();
            box.ImageLocation = s.ImageDir;
            box.BorderStyle = BorderStyle.FixedSingle;
            box.SizeMode = PictureBoxSizeMode.AutoSize;
            //box.Dock = DockStyle.Fill;


            return box;
        }

        /**
         * Create and set the properties of the button
         * also adds the button/show reference to the dictionary: buttons.
         * */
        private Button ButtonSetUp(Show s)
        {
            Button button = new Button();
            button.Click += new EventHandler(button_Click);
            button.Text = s.Date.ToShortDateString() + " - " + s.Date.ToShortTimeString();
            button.Name = "select" + s.Show_Id.ToString();
            button.Size = new System.Drawing.Size(140, 30);
            button.TextAlign = ContentAlignment.TopCenter;

            buttons.Add(button, s);

            return button;
        }

        /**
         * Assembles the controls in the TableLayoutPanel and FlowLayoutPanel respectively
         * 
         * */
        private void AssemblePanels(PictureBox box, Button button)
        {


            //create table
            TableLayoutPanel table = new TableLayoutPanel();

            //adds the image and the label to the table
            table.Controls.Add(box, 0, 0);
            table.Controls.Add(button, 0, 1);

            //sets the size of the table to the size of the image + the size of the label
            //table.Height = box.Size.Height + label.Size.Height;
            //table.Width = box.Size.Width;
            table.Width = 145;
            table.Height = 275;
            table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            //add the TableLayoutPanel to the FlowLayoutPanel
            panelMovieContainer.Controls.Add(table);
        }

        protected void button_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClick != null)
            {
                
                Button b = (Button)sender;          //grab the button that caused the event
                Show s = buttons[b];                //retrieve the show associated with said buttton

                MessageBox.Show(booking.PbList.Count.ToString());
                booking.PbList.Add(bookingService.CreateLocalPartBooking());
                int i = booking.PbList.Count - 1;                           //retrieve the newly created partbooking index
                booking.PbList[i].TheShow = s; 

                //MessageBox.Show("opret partbk efter fortryd: " + booking.PbList.Count.ToString());

                this.InnerButtonClick(sender, e);   
            }
        }

        #region Helper Methods
        /// <summary>
        /// Creates the empty vessels of a booking, and a partbooking.
        /// </summary>
        private void CreateLocals()
        {
            booking = bookingService.CreateLocalBooking();              //create an empty booking object
            
        }

        /// <summary>
        /// Loads an employee and sets his/her information on the screen
        /// </summary>
        private void LoadEmployeePic()
        {

            pictureBox1.ImageLocation = "http://www.mynetimages.com/4f992650cb.jpg";
            lblEmpName.Text = employee.FName + " " + employee.LName;
            lblEmpTitle.Text = employee.Title;
        }

        /// <summary>
        /// Retrieves a customer, and prints his/her booking information on the screen
        /// </summary>
        private void LoadBookingInfo()
        {
            lblCusName.Text = customer.FName + customer.LName;
            if (booking == null)
            {
                lblBookId.Text = "0"; //Empty booking object

            }
            else
            {
                lblBookId.Text = booking.BookingId.ToString();
            }

            if (booking.PbList.Count == 0)
            {
                lblPartId.Text = "0"; //Empty Partbooking object
            }
            else
            {
                lblPartId.Text = booking.PbList[booking.PbList.Count - 1].Id.ToString();
            }
        }
        #endregion

    }
}
